package com.zeng.bean.restmodel;

import com.zeng.annotation.restmodel.RestModel;
import com.zeng.pojo.restmodel.BaseRestModel;
import com.zeng.util.GenUtils;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;

/**
 * 用于增删改查的通用参数
 */
@Data
public class RestQueryParam {

    /**
     * 唯一标识
     */
    private Integer id;

    /**
     * 表名
     */
    private String tableName;

    /**
     * 查询字段
     */
    private List<RestQueryPropParam> selectParams = new ArrayList<>();

    /**
     * 过滤条件
     */
    private List<RestQueryPropParam> whereParams = new ArrayList<>();

    /**
     * 主类信息
     */
    private Class<? extends BaseRestModel> clazz;


    public static RestQueryParam defaultSelect(Class<? extends BaseRestModel> clazz) throws Exception {
        RestQueryParam restQueryParam = new RestQueryParam();
        restQueryParam.setClazz(clazz);

        String tableName = clazz.getSimpleName();
        RestModel annotation = clazz.getAnnotation(RestModel.class);
        if(annotation != null && !GenUtils.stringIsNullOrWhiteSpace(annotation.tableName())) {
            tableName = annotation.tableName();
        }

        restQueryParam.setTableName(tableName);
        restQueryParam.setSelectParams(RestQueryPropParam.getAllFields(clazz));
        return restQueryParam;
    }

}
